import { App, createApp } from "vue";
import Message from "@/components/Message/index.vue";
// let dialogApp: App<Element> | undefined;

// export async function showMessage(msg: string) {
// 	if (dialogApp) {
// 		return;
// 	}
// 	return new Promise((resolve, reject) => {
// 		const mountNode = document.createElement("div");
// 		dialogApp = createApp(Message, {
// 			msg,
// 			visible: true,
// 			close: () => {
// 				if (dialogApp) {
// 					dialogApp.unmount();
// 					document.body.removeChild(mountNode);
// 					dialogApp = undefined;
// 					reject("close");
// 				}
// 			},
// 			confirm: () => {
// 				resolve("confirm");
// 				dialogApp?.unmount();
// 				document.body.removeChild(mountNode);
// 				dialogApp = undefined;
// 			}
// 		});
// 		document.body.appendChild(mountNode);
// 		dialogApp.mount(mountNode);
// 	});
// }
/**
 * 全局弹窗
 *	@param msg 提示信息
 *	@returns Promise<string> | void
 */
export const showMessage = (function () {
	let dialogApp: App<Element> | undefined;
	return function (msg: string): Promise<string> | void {
		if (!dialogApp) {
			return new Promise((resolve, reject) => {
				const mountNode = document.createElement("div");
				dialogApp = createApp(Message, {
					msg,
					visible: true,
					close: () => {
						if (dialogApp) {
							dialogApp.unmount();
							document.body.removeChild(mountNode);
							dialogApp = undefined;
							reject("close");
						}
					},
					confirm: () => {
						resolve("confirm");
						dialogApp?.unmount();
						document.body.removeChild(mountNode);
						dialogApp = undefined;
					}
				});
				document.body.appendChild(mountNode);
				dialogApp.mount(mountNode);
			});
		}
	};
})();
